﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ListenMusic.AdminAlbum_ServiceReference;
using ListenMusic.Models.Reposistory.Public;
using System.ServiceModel;
using ListenMusic.Models.Reposistory.User;
using System.Transactions;
using System.IO;
using ListenMusic.Singer_ServiceReference;
using ListenMusic.Models.Reposistory.Admin;
using ListenMusic.Models.ViewModel;
using PagedList;
namespace ListenMusic.Controllers.Admin
{
    public class ManageAlbumController : AdminController
    {
        private int pagesize = 10;
        AdminUploadReposistory _uploadAdmin;
        AdminUpdateReposistory _adminUpdate;
        CountryReposistory _country ;
        CategoryReposistory _category;
        public ManageAlbumController()
        {
            _uploadAdmin = new AdminUploadReposistory();
            _adminUpdate = new AdminUpdateReposistory();
            _country = new CountryReposistory();
            _category = new CategoryReposistory();
        }
        //
        // GET: /ManageAlbum/
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult ViewAllAlbum(int? pageindex)
        {
            if (pageindex == null)
            {
                pageindex = 1;
            }
            List<AlbumSinger> lst = _manageAlbum.getAllAlbum().ToPagedList((pageindex ?? 1),pagesize).ToList();
            int count = _manageAlbum.getAllAlbum().Count();
            int totalAlbum = 0;
            if (count % pagesize == 0)
            {
                totalAlbum = (int)count / pagesize;
            }
            else
            {
                totalAlbum = (int)Math.Ceiling((double)(count) / pagesize);
            }
            ViewBag.totalAlbum = totalAlbum;
            ViewBag.pageindex = pageindex;
            ViewBag.lstAlbum = lst;
            ViewBag.count = count;
            return View();
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult UploadAlbumPage()
        {
            
            ViewBag.country = _country.getAllCountry();
            ViewBag.category = _category.getAllCategory();
            return View();
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public JsonResult deleteSonginAlbum(int songid)
        {
            bool rs = _manageAlbum.deleteSonginAlbum(songid);
            return Json(rs, JsonRequestBehavior.AllowGet);
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult ConfirmUploadAlbum(IEnumerable<HttpPostedFileBase> filename, string albumname, string singername, int country, int category, HttpPostedFileBase filepicture, string description, string releaseyear)
        {            
            string pathSong = Server.MapPath("~/Music/");
            string pathAlbumImage = Server.MapPath("~/Images/albums/");
            bool rs = _uploadAdmin.ConfirmUploadAlbum(filename, albumname, singername, country, category, filepicture, description, releaseyear, pathSong,pathAlbumImage);
            if (rs)
            {
                return RedirectToAction("NoneActiveSong", "ManageSong", new { pageindex = 1 });
            }
            else
            {
                return RedirectToAction("UploadAlbumPage");
            }            
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult UpdateAlbumPage(int albumid)
        {          
            ViewBag.country = _country.getAllCountry();
            ViewBag.category = _category.getAllCategory();
            UpdateAlbumPage uap = _manageAlbum.loadUpdateAlbumPage(albumid);
            if (uap != null)
            {
                return View(uap);
            }
            else
            {
                return RedirectToAction("ViewAllAlbum", new { pageindex = 1 });    
            }
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult ConfirmUpdateAlbum(int albumid, HttpPostedFileBase filepicture, string oldpicture, string albumname, string singername, string releaseyear, int category, int country, string description)
        {   
            string pathPicture = Server.MapPath("~/Images/albums/");
            bool rs = _adminUpdate.UpdateAlbum(albumid, filepicture, oldpicture, albumname, singername, releaseyear, category, country, description, pathPicture);
            if(rs)
            {
                return RedirectToAction("ViewAllAlbum", new {pageindex = 1 });
            }
            else
            {
                return RedirectToAction("UpdateAlbumPage", new { albumid = albumid }); 
            }
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult AddSongInAlbum(int albumid, string albumname, string singername, int category, int country)
        {           
            ViewBag.country = _country.getAllCountry();
            ViewBag.category = _category.getAllCategory();
            ViewBag.AlbumName = albumname;
            ViewBag.SingerName = singername;
            ViewBag.AlbumID = albumid;
            ViewBag.Categorye = category;
            ViewBag.Countrye = country;
            return View();
        }
        //[Authorize(Roles="WebsiteAdmin")]
        public ActionResult ConfirmAddSongInAlbum(HttpPostedFileBase filesong, int albumid, string songname, string albumname, string singername, string musician, int category, int country)
        {
            string pathMP3 = Server.MapPath("~/Music/");
            bool rs = _adminUpdate.AddSongInAlbum(filesong, albumid, singername, songname, category, country, musician, pathMP3);
            if (rs)
            {
                return RedirectToAction("UpdateAlbumPage", new { albumid = albumid });
            }
            else
            {
                return RedirectToAction("ViewAllAlbum", new {pageindex = 1 });
            }            
        }
    }
}
